package com.generator.sysManage.userManage.controller;


import com.generator.sysManage.userManage.pojo.*;
import com.generator.sysManage.userManage.service.impl.UMService;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * UMController
 * @author WJW
 * @Date .....
 * @version ......
 */
@Controller
@RequestMapping("/userManage")
public class UMController {

    @Autowired
    private UMService umService;
    public UMService getUmService() {
        return umService;
    }
    public void setUmService(UMService umService) {
        this.umService = umService;
    }


    //用户列表（分页）
    @RequestMapping("/userList")
    public String userList(Model model, Users user, PageModel<Users> pageModel){
        PageModel<Users> userList = umService.getUserListByFY(user,pageModel);
        model.addAttribute("userList",userList);
        return "sys/users/userList";
    }

    //查询所有部门
    @RequestMapping("/deptList")
    @ResponseBody
    public List<Dept> getDeptList(){
        return umService.getDeptList(new Dept());
    }


    //查询所有职位
    @RequestMapping("/jobList")
    @ResponseBody
    public List<Job> getjobList(Job job){
        return umService.getJobList(job);
    }

    //用户详情
    @RequestMapping("/userView")
    public String userView(Model model, Users1 users1){
        List<Users> users = umService.queryOne(users1);
        if(users!=null && users.size()==1){
            model.addAttribute("user",users.get(0));
            return "sys/users/userView";
        }else {
            return "redirect:/userManage/userList";
        }
    }


    //进入添加用户页面
    @RequestMapping("/userAdd")
    public String addUser(){
        return "sys/users/userAdd";
    }


    //用户编号局部验证
    @RequestMapping("/u_idYZ")
    @ResponseBody
    public String u_idYZ(Users users){
        List<Users> userLsit = umService.getUserListByUser(users);
        if(userLsit!=null&&userLsit.size()==1){
            return "true";
        }
        return "false";
    }


    //添加用户
    @RequestMapping("/addUser1")
    @ResponseBody
    public String addUser(Users1 users){
        users.setHiredate(new Date());
        Integer add = umService.addUser(users);
        if(add==1){
            return "true";
        }
        return "false";
    }




    //进入修改用户页面
    @RequestMapping("/userUpdate")
    public String userUpdate(Users user,Model model){
        List<Users> userListByUser = umService.getUserListByUser(user);
        model.addAttribute("updateUser",userListByUser.get(0));
        List<Dept> deptList = umService.getDeptList(new Dept());
        model.addAttribute("deptList1",deptList);
        List<Job> jobList = umService.getJobList(new Job());
        model.addAttribute("jobList1",jobList);
        return "sys/users/userUpdate";
    }


    //修改用户
    @RequestMapping("/updateUser1")
    @ResponseBody
    public String updateUser1(Users1 users){
        Integer add = umService.updateUser(users);
        if(add==1){
            return "true";
        }
        return "false";
    }


    //修改职位状态
    @RequestMapping("/updateStatu")
    public String updateStatu(Users users,Model model){
        return umService.updateStatu(users,model);
    }


}
